package com.cherry.openlib.datebase.litepal

import android.content.ContentValues
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.cherry.openlib.R
import kotlinx.android.synthetic.main.activity_lite_pal_test.*
import org.litepal.LitePal
import org.litepal.extension.findAllAsync
import org.litepal.extension.findAsync
import java.lang.StringBuilder
import java.util.*
import kotlin.collections.ArrayList

/**
 * 除了“查询”，所有的数据库操作都可使用 SQL 语句
 * db.executeSQL(String sql);
db.executeSQL(String sql, Object[] bindArgs);
sql语句中使用占位符，参数2 = 实际的参数集
 *
 *
 */
class LitePalTestActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lite_pal_test)
    }

    fun queryAll(view: View) {

        LitePal.findAllAsync<News>().listen { newsList ->

            Log.e("=====size", "----${newsList.size}")

            if (newsList.isNotEmpty()) {
                val builder = StringBuilder()
                newsList.forEach {
                    builder.append(it.id).append("\t").append(it.title).append("\t").append(it.content)
                            .append("\t").append(it.comments.toString()).append("\t").append("\n")
                }
                result.text = builder.toString()
            } else {
                result.text = ""
            }
        }
    }


    fun addFive(view: View) {

        val comments = ArrayList<Comment>()
        //传统,效率低下
        for (i in 1..5) {
            val comment = Comment(System.currentTimeMillis(), "王${Random().nextInt(10)}", "第$i 条评论", Date())
//            comment.saveAsync()
            comments.add(comment)
        }
        LitePal.saveAllAsync(comments).listen {
            Log.e("=====评论", "----$it")
        }

        //一次性
        val newsList = ArrayList<News>()
        for (i in 1..5) {
            newsList.add(News(System.currentTimeMillis(), comments, "标题党$i", "新闻内容$i"))
        }
        LitePal.saveAllAsync(newsList).listen {
            Log.e("=====新闻", "----$it")
        }

    }

    fun delete(view: View) {
        if (input.text.toString().isNotEmpty()) {
            LitePal.deleteAllAsync(News::class.java, "name = ${input.text}")
        }
    }

    fun queryWhere(view: View) {
        if (input.text.toString().isNotEmpty()) {
            LitePal.where("name = ${input.text}").findAsync<News>().listen { newsList ->
                if (newsList.isNotEmpty()) {
                    val builder = StringBuilder()
                    newsList.forEach {
                        builder.append(it.id).append("\t").append(it.title).append("\t").append(it.content)
                                .append("\t").append(it.comments.toString()).append("\t").append("\n")
                    }
                    result.text = builder.toString()
                } else {
                    result.text = ""
                }
            }
        }
    }

    fun update(view: View) {
        if (input.text.toString().isNotEmpty()) {
            val values = ContentValues()
            values.put("name", "王110")
            LitePal.updateAllAsync(News::class.java, values, "name = ${input.text.toString()}")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LitePal.getDatabase().close()
    }

}
